import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd, {notification} from "ant-design-vue";
import "ant-design-vue/dist/antd.css"
import axios from "axios";
import './assets/js/enums'

// createApp(App).use(Antd).use(store).use(router).mount('#app')
const app = createApp(App);
app.use(Antd).use(store).use(router).mount('#app');

/**
 * axios拦截器
 */
axios.interceptors.request.use(config => {
    console.log('请求参数：', config);
    // 拿到当前登录会员的token
    const _token = store.state.member.token;
    if (_token) {
        config.headers.token = _token;
        console.log("请求header增加token", _token);
    }
    return config;
}, error => {
    return Promise.reject(error);
});
axios.interceptors.response.use(response => {
    console.log('返回结果：', response);
    return response;
}, error => {
    console.log('返回错误：', error);
    const response = error.response;
    const status = response.status;
    // 如果状态码是401，说明没有权限访问，则跳转到登录页面
    if (status === 401) {
        console.log("未登录或登录超时，跳到登录页");
        store.commit("setMember", {});
        notification.error({description: "未登录或登录超时"});
        router.push("/login");
    }
    return Promise.reject(error);
});
axios.defaults.baseURL = process.env.VUE_APP_SERVER;
console.log("环境：", process.env.NODE_ENV);
console.log("服务地址：", process.env.VUE_APP_SERVER);